// pages/staff_work_manager/staff_work_manager.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getCategoryList, getRelationGoods } from '../../static/api/api.js'
let app = new getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageStatus:'1',
        category:null,
        curCatgoryId:'',
        workList: null,
        isNoMore:false,
        curStatus:1,
        pages: {
            curPage: 1,
            totalPage: 1
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.setData({
            'store_id':options.store_id || '0',
            'store_user_id': options.store_user_id,
            'goods_ids': options.goods_id.split(',')
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getCategoryList1()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    
    //获取分类数据
    getCategoryList1() {
        let that = this;
        console.log('加载分类')
        getCategoryList({}).then(response => {
            console.log('分类数据')
            wx.hideLoading()
            that.setData({
                'category': response.data
            })
            this.getRelationGoods()
        }).catch(response => {
            wx.hideLoading()
            console.log('数据获取失败')
            console.log(response)
        })
    },
    changeCatgory(e){
        let dataset = e.currentTarget.dataset;
        this.setData({
            curCatgoryId: dataset.id,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore':false,
            workList: null
        })
        this.getRelationGoods()
    },
    
    lower: function (e) {
        this.getRelationGoods()
    },
    getRelationGoods() {
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let that = this;
        let params={
            status: this.data.curStatus,
            category_id: this.data.curCatgoryId,
            store_id:this.data.store_id,
            store_user_id: this.data.store_user_id,
            page:this.data.pages.curPage
        }
        getRelationGoods(params).then(response => {
            console.log(response.data)
            let cArr = [];
            for (var i = 0; i < response.data.lists.length;i++){
                let c = true;
                for (var j = 0; j < this.data.goods_ids.length;j++){
                    if (response.data.lists[i].goods_id == this.data.goods_ids[j]){
                        c=false
                    }
                }
                if(c){
                    cArr.push(response.data.lists[i])
                }
                
            }
            let data = response.data;
            if (that.data.pages.curPage == 1) {
                that.setData({
                    workList: cArr
                })
            } else {
                that.setData({
                    workList: that.data.workList.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.pages.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            that.setData({
                'pageLoad': false
            })
        })
        
    },
    chooseItemCb(e){
        let dataset = e.currentTarget.dataset;
        let pages = getCurrentPages();
        let tarPage = pages[pages.length - 2];
        tarPage.chooseGoodsCb(dataset.params);
        // tarPage.setData({
        //     'goodsData': dataset.params,
        // })
        wx.navigateBack();
    }
})